<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车牌号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vehicle:vehicleHead:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['vehicle:vehicleHead:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vehicle:vehicleHead:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vehicle:vehicleHead:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="vehicleHeadList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="车牌号" align="center" prop="code"/>
      <el-table-column label="数据状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.CommDataStatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="所属组织" align="center" prop="jcOrganizationIdDtLabel"/>
      <el-table-column label="车型" align="center" prop="jcVehicleIdDtLabel"/>
      <el-table-column label="车辆所有权" align="center" prop="ownership">
        <template #default="scope">
          <dict-tag :options="dict.type.vehicle_ownership" :value="scope.row.ownership"/>
        </template>
      </el-table-column>
      <el-table-column label="司机" align="center" prop="driver"/>
      <el-table-column label="电话" align="center" prop="iphone"/>
      <el-table-column label="尺寸" align="center" prop="size"/>
      <el-table-column label="总质量" align="center" prop="totalMass"/>
      <el-table-column label="总牵引质量" align="center" prop="pullTotalMass"/>
      <el-table-column label="车架号" align="center" prop="vin"/>
      <el-table-column label="备注" align="center" prop="description"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vehicle:vehicleHead:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vehicle:vehicleHead:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆登记对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">


            <el-form-item label="车牌号" prop="code">
              <el-input v-model="form.code" placeholder="请输入车牌号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属机构" prop="jcOrganizationId">
              <el-select clearable filterable v-model="form.jcOrganizationId" placeholder="请选择所属机构"
                         @change="getDriverList">
                <el-option v-for="dict in dict.type.DB_JC_ORG" :key="dict.value" :label="dict.label"
                           :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车型" prop="jcVehicleId">
              <el-select filterable v-model="form.jcVehicleId" placeholder="请选择车型">
                <el-option v-for="dict in dict.type.DB_JC_VEHICLE" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆所有权" prop="ownership">
              <el-select filterable v-model="form.ownership" placeholder="请选择车辆所有权">
                <el-option v-for="dict in dict.type.vehicle_ownership" :key="dict.value" :label="dict.label"
                           :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="司机" prop="jcDriverId">
              <el-select filterable v-model="form.jcDriverId" placeholder="请选择司机" @change="setDriverValue">
                <el-option v-for="item in driverList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="iphone">
              <el-input v-model="form.iphone" placeholder="请输入电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总牵引质量" prop="pullTotalMass">
              <el-input v-model="form.pullTotalMass" placeholder="请输入总牵引质量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总质量" prop="totalMass">
              <el-input v-model="form.totalMass" placeholder="请输入总质量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="整车重量" prop="vehicleWeight">
              <el-input v-model="form.vehicleWeight" placeholder="请输入整车重量"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车架号" prop="vin">
              <el-input v-model="form.vin" placeholder="请输入车架号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="尺寸" prop="size">
              <el-input v-model="form.size" placeholder="请输入尺寸"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.CommDataStatus"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="description">
              <el-input v-model="form.description" placeholder="请输入备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listVehicleHead,
  getVehicleHead,
  delVehicleHead,
  addVehicleHead,
  updateVehicleHead
} from "@/api/vehicle/vehicleHead";
import {allListDriver} from "@/api/vehicle/driver";

export default {
  name: "VehicleHead",
  dicts: ['CommDataStatus', 'DB_JC_ORG', 'DB_JC_VEHICLE', 'vehicle_ownership'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆登记表格数据
      vehicleHeadList: [],
      driverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 司机id时间范围
      daterangeCreateTime: [],
      // 司机id时间范围
      daterangeModifyTime: [],
      // 司机id时间范围
      daterangeBuyTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        status: null,
        buyAddress: null,
        buyTime: null,
        displacement: null,
        emiStandard: null,
        engineVin: null,
        gearBoxType: null,
        maxKw: null,
        maxPs: null,
        maxTorque: null,
        mileage: null,
        oilSize: null,
        oilType: null,
        personNumber: null,
        price: null,
        pullTotalMass: null,
        size: null,
        source: null,
        supplierIphone: null,
        supplierName: null,
        tireNumber: null,
        tireType: null,
        totalMass: null,
        vehicleWeight: null,
        vin: null,
        ownership: null,
        jcRegistrationId: null,
        jcOrganizationId: null,
        jcOrganizationIdTheir: null,
        driver: null,
        iphone: null,
        jcDriverId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          {required: true, message: "车牌号不能为空", trigger: "blur"}
        ],
        jcOrganizationId: [
          {required: true, message: "所属机构不能为空", trigger: "blur"}
        ],
        jcVehicleId: [
          {required: true, message: "车型不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "数据状态不能为空", trigger: "change"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询车辆登记列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' !== this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeModifyTime && '' !== this.daterangeModifyTime) {
        this.queryParams.params["beginModifyTime"] = this.daterangeModifyTime[0];
        this.queryParams.params["endModifyTime"] = this.daterangeModifyTime[1];
      }
      if (null != this.daterangeBuyTime && '' !== this.daterangeBuyTime) {
        this.queryParams.params["beginBuyTime"] = this.daterangeBuyTime[0];
        this.queryParams.params["endBuyTime"] = this.daterangeBuyTime[1];
      }
      listVehicleHead(this.queryParams).then(response => {
        this.vehicleHeadList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getDriverList(v) {
      this.driverList = []
      if (v) {
        allListDriver({'jcOrganizationId': v}).then(response => {
          this.driverList = response.data;
        });
      }
    },
    setDriverValue(v) {
      if (v) {
        const item = this.driverList.filter(item => {
          return item.id === v
        })
        if (item && item.length === 1) {
          this.form.driver = item[0].name
          this.form.iphone = item[0].iphone1
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        createName: null,
        createTime: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: 1,
        buyAddress: null,
        buyTime: null,
        displacement: null,
        emiStandard: null,
        engineVin: null,
        gearBoxType: null,
        maxKw: null,
        maxPs: null,
        maxTorque: null,
        mileage: null,
        oilSize: null,
        oilType: null,
        personNumber: null,
        price: null,
        pullTotalMass: null,
        size: null,
        source: null,
        supplierIphone: null,
        supplierName: null,
        tireNumber: null,
        tireType: null,
        totalMass: null,
        vehicleWeight: null,
        vin: null,
        ownership: null,
        jcRegistrationId: null,
        jcOrganizationId: null,
        jcOrganizationIdTheir: null,
        driver: null,
        iphone: null,
        jcDriverId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeModifyTime = [];
      this.daterangeBuyTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆登记";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVehicleHead(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆登记";
        this.getDriverList(this.form.jcOrganizationId)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateVehicleHead(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVehicleHead(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆登记编号为"' + ids + '"的数据项？').then(function () {
        return delVehicleHead(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vehicle/vehicleHead/export', {
        ...this.queryParams
      }, `vehicleHead_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
